package com.ziyougou.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ziyougou.product.entity.AttrEntity;
import com.ziyougou.product.service.CategoryService;
import com.ziyougou.product.vo.AttrGroupAttrVo;
import com.ziyougou.product.vo.AttrGroupRelationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ziyougou.product.entity.AttrGroupEntity;
import com.ziyougou.product.service.AttrGroupService;
import com.ziyougou.common.utils.PageUtils;
import com.ziyougou.common.utils.R;



/**
 * 属性分组
 *
 * @author yuanhaipeng
 * @email yuanhaipeng@gmail.com
 * @date 2024-06-26 14:09:54
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    //根据分类id,查询该分类关联的属性列表(attr表)
    @GetMapping("{catId}/withattr")
    public R  getAttrListByCatId(@PathVariable("catId") Long catId){
       List<AttrGroupAttrVo> list= attrGroupService.getAttrListByCatId(catId);
        return  R.ok().put("data",list);
    }


    //保存属性分组和属性之间的关联关系
    @PostMapping("attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVO> attrGroupRelationVOList ){
          attrGroupService.saveRelation(attrGroupRelationVOList);
        return R.ok();
    }

    //查询某个分组关联的属性列表
    @RequestMapping("{attrgroupId}/attr/relation")
    public R getListByGroupId(@PathVariable("attrgroupId")Integer attrgroupId){
        List<AttrEntity> attrEntityList=attrGroupService.getListByGroupId(attrgroupId);
        return  R.ok().put("data",attrEntityList);
    }
    //查询某个分组没有关联的属性列表
    @GetMapping("{attrgroupId}/noattr/relation")
    public R getNoAttrListByGroupId(@PathVariable("attrgroupId")Integer attrgroupId,@RequestParam Map<String, Object> params){

        PageUtils page=attrGroupService.getNoAttrListByGroupId(attrgroupId,params);
        return  R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

  @Autowired
  private CategoryService categoryService;
    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
   // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        //1.根据分组id查询分组信息
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //2.根据分类id,查询该分类ID的父分类目录-调取categoryService
        Long[] path=categoryService.findParentPathByCategoryId(attrGroup.getCatelogId());
        //3.封装数据
        attrGroup.setCategoryPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }
/**
 * 根据分类id,查询该分类对应的分组信息
 */

    @GetMapping("/list/{catId}")
    public R getAttrGroupListByCatId(@RequestParam Map<String, Object> params,@PathVariable("catId") Long catId){
        PageUtils page=attrGroupService.getAttrGroupListByCatId(params,catId);
        return R.ok().put("page",page);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 根据分组id和属性id,删除数据
     */
    @PostMapping("/attr/relation/delete")
    public R deleteByAttrIdAndGroupId(@RequestBody List<AttrGroupRelationVO> attrGroupRelationVOList ){
        attrGroupService.deleteByAttrIdAndGroupId(attrGroupRelationVOList);
        return R.ok();
    }
}
